import { Inject, Provide } from '@midwayjs/core';
import { BaseService } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { MallCategoryEntity } from '../entity/category';

/**
 * 商品分类
 */
@Provide()
export class MallCategoryService extends BaseService {
  @InjectEntityModel(MallCategoryEntity)
  mallCategoryEntity: Repository<MallCategoryEntity>;

  /**
   * 获取所有分类对应的商品数量
   */
  async getCategoryGoodsCount() {
    const sql = `
      SELECT
        c.id AS categoryId,
        c.name AS categoryName,
        COUNT(g.id) AS goodsCount
      FROM
        mall_category c
      LEFT JOIN
        mall_goods g ON c.id = g.categoryId
      GROUP BY
        c.id, c.name
    `;
    return this.nativeQuery(sql);
  }
}
